import React, { Component } from 'react'
import { connect } from 'dva';
import moment from 'moment';
import { Form, Input, DatePicker, Modal, message } from 'antd';
import { SearchRow, SearchCol, SearchButton } from '@/styled/form';
import { DICT } from '../enum';
import Link from 'umi/link';
import { templateRemove } from '../service';

const { RangePicker } = DatePicker;
const { confirm } = Modal;

interface Props {
  current?: number,
  form?: any,
  dispatch?: Function,
  removeOption?: Function,
  initCurrent?: Function,
  dict?: Object,
  userIdList?: any,
  cond?: any
}

const FormItem = Form.Item;
const ONE_PAGE_SIZE = 10;

@connect(({ report_template }) => ({
  dict: report_template.dict,
  cond: report_template.cond
}))
class Search extends Component<Props, any> {
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'report_template/queryDict',
      payload: {
        keyList: DICT
      }
    }).then(() => {
      const { cond, form } = this.props;
      const {pageNum, pageSize, ...searchParams} = cond;
      form.setFieldsValue(searchParams);
    });

    dispatch({
      type: 'report_template/queryDictList'
    })
  }

  queryData = (offset = 1, size = ONE_PAGE_SIZE) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'report_template/queryPage',
      payload: {
        pageNum: offset,
        pageSize: size,
        type: '2'
      },
    });
  };

  delConfirm = () => { // 删除
    let that = this;
    let idList = that.props.userIdList;
    if(idList.length === 0) {
      message.warning('请选择后操作', 1.5);
      return;
    }
    confirm({
      title: '删除',
      content: '是否删除？',
      onOk() {
        let params = {
          idList
        };
        let tipMsg = message.loading('');
        return templateRemove(params).then(() => {
          tipMsg();
          message.success('操作成功', 1.5);
          that.props.removeOption('ok');
          that.queryData();
        }, tipMsg);
      },
      onCancel() {},
      okText: "确认",
      cancelText: "取消"
    });
  };

  search = e => { e.preventDefault();
    const { dispatch } = this.props;
    this.props.form.validateFields((err, values) => {
     if(!!values.createTime) {
       values.createTimeStart = moment(values.createTime[0]._d).format("YYYY-MM-DD");
       values.createTimeEnd = moment(values.createTime[1]._d).format("YYYY-MM-DD");
     }
     delete values.createTime;
      if (!err) {
        dispatch({
          type: 'report_template/updateCond',
          payload: {
            pageNum: 1,
            pageSize: ONE_PAGE_SIZE,
            type: '2',
            ...values
          }
        });

        this.props.initCurrent()
      }
    });
  };

  render() {
    const { form } = this.props;
    const { getFieldDecorator } = form;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
    };
    return (
      <Form onSubmit={this.search}>
        <SearchRow>
          <SearchCol key={0}>
            <FormItem
              label="名称"
              {...formItemLayout}
            >
              {getFieldDecorator('name', {
                rules: [{ message: '输入名称' }],
              })(
                <Input style={{width: '160px'}} placeholder="请输入名称" />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={1}>
            <FormItem
              label="创建时间"
              {...formItemLayout}
            >
              {getFieldDecorator('createTime')(
                <RangePicker
                  format="YYYY-MM-DD"
                  placeholder={['开始日期', '结束日期']}
                />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={3}>
            <SearchButton htmlType="submit">搜索</SearchButton>
            <SearchButton onClick={(e) => {
              this.props.form.resetFields();
              this.search(e)
            }} style={{ 'marginLeft': '15px' }}>重置</SearchButton>
          </SearchCol>
          <SearchCol key={4}>
            <Link to='/data/report_template/new_template'><SearchButton icon='plus'>新增模板</SearchButton></Link>
          </SearchCol>
          <SearchCol key={5}>
            <SearchButton icon='minus' onClick={this.delConfirm}>删除模板</SearchButton>
          </SearchCol>
        </SearchRow>
      </Form>
    );
  }
}

const SearchS = Form.create<any>()(Search);
export default SearchS;
